/*********************************************/
/*                                           */
/*  This program performs a job mobility     */
/*  analysis on the post-estimation          */
/*  sample.                                  */
/*                                           */
/*  Before running this we need to make      */
/*  the appropriate text file prinouts       */
/*  of the CHK estimation. These printouts   */
/*  include all the estimated fixed effects  */
/*                                           */
/*  Also need annual files that include      */
/*  the number of jobs per worker per year.  */
/*                                           */
/*                                           */
/*********************************************/

%let basedir = <filepath>; /* matlab estimation output dataset */
%let datadir = <filepath>;               /* annual files with firm info      */
%let outputdir = <filepath>;  /* where the output files go        */
%let sample = S100P;           /* S10P or S100P        */
%let earnval = 1_520hr;
%let popval = Male;            /* Male, Female, or AllSex       */
%let firmswtval = 1;          /* Controls how many job switchers a firm has */

/*********************************************/
/*                                           */
/*  0.) Create wide dataset with # jobs.     */
/*                                           */
/*********************************************/
libname adata "&datadir.";
%macro datastep(earn,pop,firmswt);
%do I=1 %to  5;
%if &I=1 %then %do;%let FY=1980; %let LY=1986;%end;
%if &I=2 %then %do;%let FY=1987; %let LY=1993;%end;
%if &I=3 %then %do;%let FY=1994; %let LY=2000;%end;
%if &I=4 %then %do;%let FY=2001; %let LY=2007;%end;
%if &I=5 %then %do;%let FY=2007; %let LY=2013;%end;
proc import datafile="&basedir.\&sample._AKMeffs_&earn._&pop._Int7_&I..txt"
	out=fe_out
	DBMS=TAB
	replace;
	getnames=no;
run;
data fe_out;
	set fe_out;
	rename var1=id var2=firmid var3=year var4=pe var5=fe var6=xb var7=r var8=y var9=yob;
	age=var3-var9;
run;
/***********************************/
/*  MERGE IN NUMBER OF JOBS        */
/***********************************/
proc sort data=fe_out;
	by year id;
run;
data njobs;
	set %do K=&FY %to &LY; adata.year&K (keep=idd year age n_jobs) %end;;
	where 20<=age<=60; rename idd=id; drop age; run;
data fe_out;
	merge fe_out njobs;
	by year id;
	if firmid^=.;
run;
/***************************************/
/*  Compute the number of worker-      */
/*  average-firm-earning comparisons   */
/***************************************/
proc sort data=fe_out;	by id firmid;run;
data i_comp;
	set fe_out; by id firmid;
	if first.firmid then x=1;
	if first.id then ic_firm=0; ic_firm+x;
    if last.id;
	ic_firm=ic_firm-1;
	keep id ic_firm;
run;
data fe_out;
	merge fe_out i_comp;
	by id;
run;
proc sort data=fe_out;
	by firmid id;
run;
data f_comp;
	set fe_out; by firmid id;
	if first.id then x=ic_firm;
	if first.firmid then c_firm=0; c_firm+x;
    if last.firmid;
	keep firmid c_firm;
run;
proc sort data=fe_out;
	by firmid;
run;
data fe_out;
	merge fe_out f_comp;
	by firmid;
	drop ic_firm;
run;
/*********************************************/
/*                                           */
/*      SET RESTRICTION ON THE NUMBER        */
/*      OF WORKER-MEAN-FIRM-EARNING          */
/*      COMPARISONS THE FIRMS HAVE.          */
/*                                           */
/*********************************************/
data fe_out;
	set fe_out;
	if c_firm>=&firmswt;
run;
/**************************************/
/*  Compute firm leave-out mean wage  */
/**************************************/
proc sort data=fe_out;by year firmid; run;
data firm_wage;
	set fe_out;
	by year firmid;
	if first.firmid then firm_n=0;
	if first.firmid then firm_y=0;
	firm_n+1;
	firm_y+y;
	if last.firmid;
	keep firmid year firm_n firm_y;
run;
data fe_out;
	merge fe_out firm_wage;
	by year firmid;
	m_f_y=(firm_y-y)/(firm_n-1);
run;
/*************************************/
/* Compute firm mean earnings        */
/* and mean fixed effect quartiles   */
/*************************************/
proc rank data=fe_out out=fe_out ties=high fraction;
	by year;
	var m_f_y fe;
   	ranks m_f_y_rank fe_rank;
run;
data fe_out;
	set fe_out;
	if .00<m_f_y_rank<=.25 then m_f_y_q=1;
	if .25<m_f_y_rank<=.50 then m_f_y_q=2;
	if .50<m_f_y_rank<=.75 then m_f_y_q=3;
	if .75<m_f_y_rank<=1   then m_f_y_q=4;
	if .00<fe_rank<=.25 then fe_q=1;
	if .25<fe_rank<=.50 then fe_q=2;
	if .50<fe_rank<=.75 then fe_q=3;
	if .75<fe_rank<=1   then fe_q=4;
	keep id yob firmid year pe fe xb y m_f_y m_f_y_q fe_q firm_n n_jobs;
run;
/*************************************/
/* Convert long to wide data set     */
/*************************************/
data fe_out;
	set fe_out;
	s_year=&FY;
	year=year-s_year+1;
run;
proc sort data=fe_out;
	by id year;
run;
data fe_out;
	set fe_out;
	by id;

	keep id yob s_year y1-y7 firmid1-firmid7 pe1-pe7 fe1-fe7 m_f_y_q1-m_f_y_q7 fe_q1-fe_q7 firm_n1-firm_n7 xb1-xb7 n_jobs1-n_jobs7;
	retain y1-y7 firmid1-firmid7 pe1-pe7 fe1-fe7 m_f_y_q1-m_f_y_q7 fe_q1-fe_q7 firm_n1-firm_n7 xb1-xb7 n_jobs1-n_jobs7;

	array ay(1:7) y1-y7 ;
	array afirmid(1:7) firmid1-firmid7 ;
	array ape(1:7) pe1-pe7 ;
	array afe(1:7) fe1-fe7 ;
	array am_f_y_q(1:7) m_f_y_q1-m_f_y_q7 ;
	array afe_q(1:7) fe_q1-fe_q7 ;
	array afirm_n(1:7) firm_n1-firm_n7 ;
	array axb(1:7) xb1-xb7 ;
	array an_jobs(1:7) n_jobs1-n_jobs7 ;

	if first.id then 
	do;
		do i = 1 to 7 ; 
			ay( i ) = . ;
			afirmid( i ) = . ;
			ape( i ) = . ;
			afe( i ) = . ;
			am_f_y_q( i ) = . ;
			afe_q( i ) = . ;
			afirm_n( i ) = . ;
			axb( i ) = . ;
			an_jobs( i ) = . ;
		end;
	end;

	ay( year ) = y ;
	afirmid( year ) = firmid ;
	ape( year ) = pe ;
	afe( year ) = fe ;
	am_f_y_q( year ) = m_f_y_q ;
	afe_q( year ) = fe_q ;
	afirm_n( year ) = firm_n ;
	axb( year ) = xb ;
	an_jobs( year ) = n_jobs ;

	if last.id then output ;
run;
/**************************************/
/*  Save dataset                      */
/**************************************/
data data_&earn._&pop._SW&firmswt._&I;
	set fe_out;
	interval=&I;
run;
proc sort data=data_&earn._&pop._SW&firmswt._&I;by id; run;
%end;
%mend;
%datastep(&earnval,&popval,&firmswtval);
/*********************************************/
/*                                           */
/*  1.) Raw switches.                        */
/*                                           */
/*     - 3 years at firm A then              */
/*       3 years at firm B.                  */
/*                                           */
/*     - 2 year job switch period            */
/*                                           */
/*********************************************/
libname adata "&datadir.";
%macro mob1(earn,pop,firmswt);
%do I=1 %to  5;
/**************************************/
/*  Create switchers sample           */
/**************************************/
data fe_out;
	set data_&earn._&pop._SW&firmswt._&I;
	if firmid1=firmid2=firmid3 & firmid4=firmid5=firmid6 & firmid3^=firmid4 & 
		m_f_y_q1^=. & m_f_y_q2^=. & m_f_y_q5^=. & m_f_y_q6^=. then case=1; /* job switchers: period 3 to 4 */
	if firmid2=firmid3=firmid4 & firmid5=firmid6=firmid7 & firmid4^=firmid5 & 
		m_f_y_q2^=. & m_f_y_q3^=. & m_f_y_q6^=. & m_f_y_q7^=. then case=2; /* job switchers: period 4 to 5 */
	if case=1 or case=2; /* keep only those eligible */
	do s=1 to 4; /* assign values to switch types */
		do e=1 to 4;
			if case=1 & m_f_y_q2=s & m_f_y_q5=e then swt_m_y=10*s+e;
			if case=2 & m_f_y_q3=s & m_f_y_q6=e then swt_m_y=10*s+e;
			if case=1 & fe_q2=s & fe_q5=e then swt_fe=10*s+e;
			if case=2 & fe_q3=s & fe_q6=e then swt_fe=10*s+e;
		end;
	end;
	output;
run;
/*************************************/
/* Compute annual mean earnings      */
/* in switch categories.             */
/* Also compute mean firm size and   */
/* detrened mean earnings.           */
/*************************************/
%macro meany(var);
proc sort data=fe_out;
	by case swt_&var.;
run;
proc means data=fe_out noprint nway;
	class s_year case swt_&var.;
	var y1-y7 firm_n1-firm_n7 xb1-xb7;
	output out=mean_y_dec_&var. mean(y1)=m_y1 mean(y2)=m_y2 mean(y3)=m_y3 mean(y4)=m_y4 mean(y5)=m_y5 mean(y6)=m_y6 mean(y7)=m_y7
					  mean(xb1)=m_xb1 mean(xb2)=m_xb2 mean(xb3)=m_xb3 mean(xb4)=m_xb4 mean(xb5)=m_xb5 mean(xb6)=m_xb6 mean(xb7)=m_xb7
					  mean(firm_n1)=m_nw1 mean(firm_n2)=m_nw2 mean(firm_n3)=m_nw3 mean(firm_n4)=m_nw4 mean(firm_n5)=m_nw5 mean(firm_n6)=m_nw6  mean(firm_n7)=m_nw7;
run;
data mean_y_dec_&var._&I;
	retain interval;
	set mean_y_dec_&var.;
	rename _freq_=n;
	interval=&I;
	array am_y(1:7) m_y1-m_y7;
	array am_xb(1:7) m_xb1-m_xb7;
	array adt_m_y(1:7) dt_m_y1-dt_m_y7;
	do i=1 to 7;
		adt_m_y(i)=am_y(i)-am_xb(i);
	end;
	output;
	drop _type_ m_xb1 m_xb2 m_xb3 m_xb4 m_xb5 m_xb6 m_xb7 i;
run;
%mend;
%meany(m_y);
%meany(fe);
data mean_y_&I;
	retain interval s_year case swt_m_y swt_fe;
	set mean_y_dec_m_y_&I mean_y_dec_fe_&I;
run;
%end;
/************************/
/* Export as CSV        */
/************************/
data mean_y;
	set mean_y_1-mean_y_5;
run;
proc export data=mean_y
	outfile="&outputdir.\&sample._JS2Year_&earn._&pop._Int7.txt"
	DBMS=CSV
	replace;
	putnames=yes;
run;
%mend;
%mob1(&earnval,&popval,&firmswtval);
/**********************************************/
/*                                            */
/*  2.) Estimate year of job switch.          */
/*                                            */
/*     - 2 years at firm A, 1 year of switch, */
/*       then 2 years at firm B.              */
/*                                            */
/*     - 1 year job switch period             */
/*                                            */
/**********************************************/
libname adata "&datadir.";
%macro mob1(earn,pop,firmswt);
%do I=1 %to  5;
/**************************************/
/*  Create switchers sample           */
/**************************************/
data fe_out;
	set data_&earn._&pop._SW&firmswt._&I;
	if firmid1=firmid2 & firmid3=firmid4=firmid5 & firmid2^=firmid3 & 
	   n_jobs3>=n_jobs2 & m_f_y_q1^=. & m_f_y_q2^=. & m_f_y_q4^=. & m_f_y_q5^=. then case=1; /* job switchers: period 3 to 4 */
	if firmid2=firmid3 & firmid4=firmid5=firmid6 & firmid3^=firmid4 & 
	   n_jobs4>=n_jobs3 & m_f_y_q2^=. & m_f_y_q3^=. & m_f_y_q5^=. & m_f_y_q6^=. then case=2; /* job switchers: period 4 to 5 */
	if firmid1=firmid2=firmid3 & firmid4=firmid5 & firmid3^=firmid4 & 
	   n_jobs3>n_jobs4 & m_f_y_q1^=. & m_f_y_q2^=. & m_f_y_q4^=. & m_f_y_q5^=. then case=1; /* job switchers: period 3 to 4 */
	if firmid3=firmid4 & firmid5=firmid6=firmid7 & firmid4^=firmid5 & 
	   n_jobs5>=n_jobs4 & m_f_y_q3^=. & m_f_y_q4^=. & m_f_y_q6^=. & m_f_y_q7^=. then case=3; /* job switchers: period 5 to 6 */
	if firmid2=firmid3=firmid4 & firmid5=firmid6 & firmid4^=firmid5 & 
	   n_jobs4>n_jobs5 & m_f_y_q2^=. & m_f_y_q3^=. & m_f_y_q5^=. & m_f_y_q6^=. then case=2; /* job switchers: period 4 to 5 */
	if firmid3=firmid4=firmid5 & firmid6=firmid7 & firmid5^=firmid6 & 
	   n_jobs5>n_jobs6 & m_f_y_q3^=. & m_f_y_q4^=. & m_f_y_q6^=. & m_f_y_q7^=. then case=3; /* job switchers: period 5 to 6 */
	if case^=.; /* keep only those eligible */
	do s=1 to 4; /* assign values to switch types */
		do e=1 to 4;
			if case=1 & m_f_y_q2=s & m_f_y_q4=e then swt_m_y=10*s+e;
			if case=2 & m_f_y_q3=s & m_f_y_q5=e then swt_m_y=10*s+e;
			if case=3 & m_f_y_q4=s & m_f_y_q6=e then swt_m_y=10*s+e;
			if case=1 & fe_q2=s & fe_q4=e then swt_fe=10*s+e;
			if case=2 & fe_q3=s & fe_q5=e then swt_fe=10*s+e;
			if case=3 & fe_q4=s & fe_q6=e then swt_fe=10*s+e;
		end;
	end;
	output;
run;
/*************************************/
/* Compute annual mean earnings      */
/* in switch categories.             */
/* Also compute mean firm size and   */
/* detrened mean earnings.           */
/*************************************/
%macro meany(var);
proc sort data=fe_out;
	by case swt_&var.;
run;
proc means data=fe_out noprint nway;
	class s_year case swt_&var.;
	var y1-y7 firm_n1-firm_n7 xb1-xb7;
	output out=mean_y_dec_&var. mean(y1)=m_y1 mean(y2)=m_y2 mean(y3)=m_y3 mean(y4)=m_y4 mean(y5)=m_y5 mean(y6)=m_y6 mean(y7)=m_y7
					  mean(xb1)=m_xb1 mean(xb2)=m_xb2 mean(xb3)=m_xb3 mean(xb4)=m_xb4 mean(xb5)=m_xb5 mean(xb6)=m_xb6 mean(xb7)=m_xb7
					  mean(firm_n1)=m_nw1 mean(firm_n2)=m_nw2 mean(firm_n3)=m_nw3 mean(firm_n4)=m_nw4 mean(firm_n5)=m_nw5 mean(firm_n6)=m_nw6  mean(firm_n7)=m_nw7;
run;
data mean_y_dec_&var._&I;
	retain interval;
	set mean_y_dec_&var.;
	rename _freq_=n;
	interval=&I;
	array am_y(1:7) m_y1-m_y7;
	array am_xb(1:7) m_xb1-m_xb7;
	array adt_m_y(1:7) dt_m_y1-dt_m_y7;
	do i=1 to 7;
		adt_m_y(i)=am_y(i)-am_xb(i);
	end;
	output;
	drop _type_ m_xb1 m_xb2 m_xb3 m_xb4 m_xb5 m_xb6 m_xb7 i;
run;
%mend;
%meany(m_y);
%meany(fe);
data mean_y_&I;
	retain interval s_year case swt_m_y swt_fe;
	set mean_y_dec_m_y_&I mean_y_dec_fe_&I;
run;
%end;
/************************/
/* Export as CSV        */
/************************/
data mean_y;
	set mean_y_1-mean_y_5;
run;
proc export data=mean_y
	outfile="&outputdir.\&sample._JS1Year_&earn._&pop._Int7.txt"
	DBMS=CSV
	replace;
	putnames=yes;
run;
%mend;
%mob1(&earnval,&popval,&firmswtval);
